package fr.azzale.casu.metier;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.text.SpannableString;
import android.util.Log;
import fr.azzale.casu.utils.Utils;

public class Line {
	public static SimpleDateFormat df = new SimpleDateFormat("HH:mm - dd/MM");
	private Casu owner;
	private Date time;
	private String text;
	private int number;
	private String timeStr;
	private String formattedTime;
	public Line(int number)
	{
		text = "";
		this.number = number;
	}
	public boolean hasOwner() {
		return owner != null;
	}
	public Casu getOwner()
	{
		return owner;
	}
	public void setOwner(Casu casu) {
		this.owner = casu;
	}

	public boolean hasTime() {
		return timeStr!=null;
	}

	public void setTime(Date parse) {
		this.time = parse;
	}

	public void setText(String str) {
		this.text = str;
	}
	public String getText() {
		return text;
	}
	public SpannableString getSpannableText()
	{
		return new SpannableString(text);
	}
	public String getFormattedDate() {
		if(formattedTime==null && timeStr==null)
		{
			return "n/a";
		}
		else if(formattedTime == null)
		{
			try{
			//[15|Jul 05:39 pm]
			Calendar c = Calendar.getInstance();
			c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(timeStr.substring(1, 3)));
			c.set(Calendar.MONTH, Utils.getMonth(timeStr.substring(4, 7)));
			c.set(Calendar.HOUR, Integer.parseInt(timeStr.substring(8,10)));
			c.set(Calendar.MINUTE, Integer.parseInt(timeStr.substring(11,13)));
			String ampm = timeStr.substring(14, 16);
			if(ampm!=null && ampm.equals("am"))
				c.set(Calendar.AM_PM, Calendar.AM);
			else
				c.set(Calendar.AM_PM, Calendar.PM);
			this.time = c.getTime();
			formattedTime =  df.format(time);
			}
			catch(Exception e)
			{
				e.printStackTrace();
				Log.e("error de date", ""+timeStr);
				return "n/a";
			}
		}
		return formattedTime;
	}
	public Date getDate() {
		return time;
	}
	public void setTime(String str) {
		this.timeStr = str;
	}

}
